package com.ktjy.ktmall.servlet;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ktjy.ktmall.entity.AjaxResult;
import com.ktjy.ktmall.entity.WxUser;
import com.ktjy.ktmall.service.WxUserService;
import com.ktjy.ktmall.service.impl.WxUserServiceImpl;
import com.ktjy.ktmall.util.UploadAliYunUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.stream.Collectors;

@WebServlet(name = "FileServlet", urlPatterns = "/uni/wxUser/file")
@MultipartConfig
public class FileServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Part part = request.getPart("file");
        Map<String, String> map = UploadAliYunUtil.uploadImage(part);
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(map));
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
